package com.ruoyi.oa.controller;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.domain.OaHumanReplace;
import com.ruoyi.oa.domain.vo.OaHumanReplaceVo;
import com.ruoyi.oa.domain.bo.OaHumanReplaceBo;
import com.ruoyi.oa.service.IOaHumanReplaceService;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.oa.domain.vo.OaProjectVo;


/**
 * 补卡申请
 *
 * @author ruoyi
 * @date 2023-08-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/humanReplace")
public class OaHumanReplaceController extends BaseController {

    private final IOaHumanReplaceService iOaHumanReplaceService;

    /**
     * 查询补卡申请列表
     */
//    @SaCheckPermission("oa:humanReplace:list")
    @GetMapping("/list")
    public TableDataInfo<OaHumanReplaceVo> list(OaHumanReplaceBo bo, PageQuery pageQuery) {
        return iOaHumanReplaceService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出补卡申请列表
     */
//    @SaCheckPermission("oa:humanReplace:export")
    @Log(title = "补卡申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaHumanReplaceBo bo, HttpServletResponse response) {
        List<OaHumanReplaceVo> list = iOaHumanReplaceService.queryList(bo);
        ExcelUtil.exportExcel(list, "补卡申请", OaHumanReplaceVo.class, response);
    }

    /**
     * 获取补卡申请详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("oa:humanReplace:query")
    @GetMapping("/{id}")
    public R<OaHumanReplaceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iOaHumanReplaceService.queryById(id));
    }

    /**
     * 新增补卡申请
     */
//    @SaCheckPermission("oa:humanReplace:add")
    @Log(title = "补卡申请", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<OaHumanReplaceBo> add(@Validated(AddGroup.class) @RequestBody OaHumanReplaceBo bo) {
        // 将Date对象转换为LocalDate对象
        LocalDate localDate = bo.getReplaceTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate now = LocalDate.now();
        // 比较两个日期的大小
        int result = localDate.compareTo(now);
        if (result >0){
            return R.fail("补卡时间异常");
        }else if (result <0){
            OaHumanReplaceBo oaHumanReplaceBo = iOaHumanReplaceService.insertByBo(bo);
            if (ObjectUtil.isEmpty(oaHumanReplaceBo)){
                return R.fail("剩余补卡次数已为0");
            }
            return R.ok(oaHumanReplaceBo);
        }else {
            return R.fail("不允许对当天打卡数据进行补卡");
        }
    }

    /**
     * 修改补卡申请
     */
//    @SaCheckPermission("oa:humanReplace:edit")
    @Log(title = "补卡申请", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<OaHumanReplace> edit(@Validated(EditGroup.class) @RequestBody OaHumanReplaceBo bo) {
        // 将Date对象转换为LocalDate对象
        LocalDate localDate = bo.getReplaceTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate now = LocalDate.now();
        boolean isSameDay = localDate.isEqual(now);
        if (isSameDay){
            return R.fail("不允许对当天打卡数据进行补卡");
        }
        OaHumanReplace oaHumanReplace = iOaHumanReplaceService.updateByBo(bo);
        if (ObjectUtil.isEmpty(oaHumanReplace)){
            return R.fail("剩余补卡次数已为0");
        }
        return R.ok(oaHumanReplace);
    }

    /**
     * 删除补卡申请
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("oa:humanReplace:remove")
    @Log(title = "补卡申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iOaHumanReplaceService.deleteWithValidByIds(Arrays.asList(ids), true));
    }


    /***
     * 通过月份和id获取本月补卡次数
     * @param userId
     * @param month
     * @return
     */

    @GetMapping("/getUserReplaceByMonth")
    public R<Integer> getUserReplaceByMonth(@RequestParam(value = "userId", required = false) String userId,
                                               @RequestParam(value = "month", required = false) String month){
        return R.ok(iOaHumanReplaceService.getUserReplaceByMonth(month,userId));
    }
}
